﻿#include "SoundGradeWgt.h"
#include "ui_SoundGradeWgt.h"
#include <QTextStream>
#include <QFileDialog>
#include <QFile>
#include <QSettings>
#include <QDateTime>
#include <QMessageBox>
SoundGradeWgt::SoundGradeWgt(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SoundGradeWgt)

{
    ui->setupUi(this);
    connect(&timer, SIGNAL(timeout()), SLOT(onTimer()));
    result = FMOD_System_Create(&system);
    result = FMOD_System_Init(system, 32, FMOD_INIT_NORMAL, 0);
    sound = 0;
    channel = 0;
    wav.resize(2048);

    _curvePlot =new CurvePlot(this);
    ui->content->addWidget(_curvePlot);
    _curvePlot->setAdjustVal(0.5);
    _curvePlot->outPut();

    initSliderQSS();

    QString fileName ="d:/test-file/kkry.wav";

}

SoundGradeWgt::~SoundGradeWgt()
{
    stopPlay();
    delete ui;
}
void SoundGradeWgt::onTimer()
{
    float *dat = new float[16384];
    FMOD_System_GetWaveData(system, dat, 16384, 0);
    //FMOD_System_GetSpectrum(system, dat, 8192, 1, FMOD_DSP_FFT_WINDOW_RECT); 频谱
    for (int i = 0, n = 0; i < 16384; i+= 8, n++)
        wav[n] = dat[i];
    _curvePlot->outPut(wav);
    delete [] dat;
    if (channel)
    {
        uint pos;
        FMOD_Channel_GetPosition(channel, &pos, FMOD_TIMEUNIT_MS);
        ui->horizontalSlider->setValue(pos);
        pos /= 1000;
        ui->label_now->setText(QString().sprintf(" %02d:%02d", pos/60, pos%60));
        FMOD_BOOL isPlay;
        FMOD_Channel_IsPlaying(channel, &isPlay);
        if (!isPlay)
        {
            timer.stop();
        }
    }
}

void SoundGradeWgt::stopPlay()
{
    if (channel)
    {
        FMOD_Channel_Stop(channel);
        channel = NULL;
        timer.stop();
        ui->label_now->setText(" 00:00");
        ui->label_total->setText("00:00 ");
        ui->horizontalSlider->setValue(0);
        _curvePlot->outPut();
    }
}
void SoundGradeWgt::initSliderQSS()
{
    ui->horizontalSlider->setStyleSheet("QSlider::groove:horizontal {  \
                                        border: 1px solid #4A708B;  \
            background: #C0C0C0;  \
height: 5px;  \
    border-radius: 1px;  \
    padding-left:-1px;  \
    padding-right:-1px;  \
}  \
\
QSlider::sub-page:horizontal {  \
                      background: qlineargradient(x1:0, y1:0, x2:0, y2:1, \
                                                  stop:0 #B1B1B1, stop:1 #c4c4c4);  \
background: qlineargradient(x1: 0, y1: 0.2, x2: 1, y2: 1,  \
                            stop: 0 #5DCCFF, stop: 1 #1874CD);  \
border: 1px solid #4A708B;  \
height: 10px;  \
border-radius: 2px;  \
}  \
\
QSlider::add-page:horizontal {  \
                      background: #575757;  \
border: 0px solid #777;  \
height: 10px;  \
border-radius: 2px;  \
}  \
\
QSlider::handle:horizontal  \
{  \
                    background: qradialgradient(spread:pad, cx:0.5, cy:0.5, radius:0.5, fx:0.5, fy:0.5,  \
                                                stop:0.6 #45ADED, stop:0.778409 red);  \
\
width: 11px;  \
margin-top: -3px;  \
margin-bottom: -3px;  \
border-radius: 5px;  \
}  \
\
QSlider::handle:horizontal:hover {  \
                    background: qradialgradient(spread:pad, cx:0.5, cy:0.5, radius:0.5, fx:0.5, fy:0.5, stop:0.6 #2A8BDA, \
                                                stop:0.778409 red);  \
width: 11px;  \
margin-top: -3px;  \
margin-bottom: -3px;  \
border-radius: 5px;  \
}  \
\
QSlider::sub-page:horizontal:disabled {  \
                      background: #00009C;  \
border-color: #999;  \
}  \
\
QSlider::add-page:horizontal:disabled {  \
                      background: #eee;  \
border-color: #999;  \
}  \
\
QSlider::handle:horizontal:disabled {  \
                    background: #eee;  \
border: 1px solid #aaa;  \
border-radius: 4px;  \
}  ");
}
void SoundGradeWgt::plotAllWav(char *fileName)
{
    FMOD_System_CreateSound(system, fileName, FMOD_2D | FMOD_SOFTWARE | FMOD_CREATESAMPLE, 0, &sound);
    uint bytes, len1, len2;
    void *ptr1, *ptr2;
    FMOD_Sound_GetLength(sound, &bytes, FMOD_TIMEUNIT_PCMBYTES);
    FMOD_Sound_Lock(sound, 0, bytes, &ptr1, &ptr2, &len1, &len2);

    bytes /= 4;
    int step= 1, len= bytes;
    if (bytes > 10000)
    {
        len = 10000;
        step= (int)(bytes / len);
    }
    QVector<float>wavAll(10000);
    short* ps = (short*)ptr1;
    for (int i= 0, n = 0; n< len; i+= step, n++)
        wavAll[n] = ps[i];
    FMOD_Sound_Unlock(sound, ptr1, ptr2, len1, len2);
    FMOD_Sound_Release(sound);
    _curvePlot->outPut(wavAll);
}
void SoundGradeWgt::Play()
{
    QString strFile = "d:/test-file/kkry.mp3";

    if (!QFile::exists(strFile))
    {
        QMessageBox::warning(this,"提示","文件不存在！");
        return;
    }
    plotAllWav(strFile.toLocal8Bit().data());
    FMOD_System_CreateSound(system, strFile.toLocal8Bit().data(), FMOD_SOFTWARE | FMOD_CREATESAMPLE, 0, &sound);
    FMOD_Sound_SetMode(sound, FMOD_LOOP_OFF);
    FMOD_System_PlaySound(system, FMOD_CHANNEL_FREE, sound, false, &channel);
    uint length;
    FMOD_Sound_GetLength(sound, &length, FMOD_TIMEUNIT_MS);
    ui->horizontalSlider->setMaximum(length);
    length /= 1000;
    ui->label_total->setText(QString().sprintf("%02d:%02d ", length/60, length%60));
    FMOD_System_Update(system);
    timer.start(400);
}
void SoundGradeWgt::on_pushButton_clicked()
{
    if(isPlaying)
    {
        pausePlay();
        ui->pushButton->setToolTip(tr("Play"));
        ui->pushButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
    }
    else
    {
        Play();
        ui->pushButton->setToolTip(tr("Pause"));
        ui->pushButton->setIcon(style()->standardIcon(QStyle::SP_MediaPause));
    }
    isPlaying =!isPlaying;

}
void SoundGradeWgt::pausePlay()
{
    if (channel)
    {
        FMOD_BOOL pause;
        FMOD_Channel_GetPaused(channel, &pause);
        FMOD_Channel_SetPaused(channel, !pause);
        (!pause) ? timer.stop() : timer.start(300);
    }
}
void SoundGradeWgt::on_pbt_pre_clicked()
{



}

void SoundGradeWgt::on_pushButton_2_clicked()
{

}

void SoundGradeWgt::on_horizontalSlider_sliderMoved(int position)
{
    if (channel) FMOD_Channel_SetPosition(channel, position, FMOD_TIMEUNIT_MS);
}
